package com.tsinkai.ettp.practice;

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;	

public class Base64Pra {
	
	private static String src = "hello world!";
	
	public static void main(String[] args) {
		jdkBase64();
		commonsCodesBase64();
		bouncyCastleBase64();
	}
	
	/**
	 * jdk自带的base64编码，需要在eclipse里打开相应的包,jdk内部使用，不建议使用。
	 */
	public static void jdkBase64() {
		
		try {
			//编码
			BASE64Encoder  encoder = new BASE64Encoder();
			String encodeStr = encoder.encode(src.getBytes());
			System.out.println("jdk encode:" + encodeStr);
			//解码
			BASE64Decoder decoder = new BASE64Decoder();
			System.out.println("jdk decode:"+new String(decoder.decodeBuffer(encodeStr)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * commonsCodes中的base64编码
	 */
	public static void commonsCodesBase64() {
		//编码
		 byte[] encodeBytes = Base64.encodeBase64(src.getBytes());
		 String baseStr = Base64.encodeBase64String(src.getBytes());
		 System.out.println("cc encode:" + new String(encodeBytes)+"||"+baseStr);
		 
		 //解码
		 byte[] decodeBytes = Base64.decodeBase64(encodeBytes);
		 System.out.println("cc decode:" + new String(decodeBytes));
	}
	
	/**
	 * bouncyCastle中的base64编码
	 */
	public static void bouncyCastleBase64() {
		//编码
		byte[] encodeBatys =  org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
		System.out.println("bc encode：" + new String(encodeBatys));
		
		//解码
		System.out.println("bc decode:" + new String(org.bouncycastle.util.encoders.Base64.decode(encodeBatys)));
	}
}
